package com.zbs.framework.core.role.action;


import com.zbs.framework.common.utils.ResultData;
import com.zbs.framework.common.utils.ResultUtil;
import com.zbs.framework.dao.role.entity.Role;
import com.zbs.framework.dao.role.model.RoleListItem;
import com.zbs.framework.dao.role.params.RoleParam;
import com.zbs.framework.dao.role.service.RoleService;
import com.zbs.framework.dao.role.view.RoleView;
import com.zbs.framework.swagger.annotations.ApiOperation;
import com.zbs.framework.swagger.annotations.ApiParam;
import com.zbs.framework.swagger.annotations.ApiTags;
import com.zbs.framework.swagger.enums.RequestType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Create by Zbs on 2018/11/23.
 */
@ApiTags(value = "角色管理", group = "UIM")
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 添加角色
     *
     * @param name
     * @param enabled
     * @param description
     * @return
     */
    @ApiOperation(value = "添加角色",response = RoleView.class)
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('sys::addRole')")
    public ResultData<RoleListItem> add(@ApiParam(value = "角色名称", required = true) String name,
                          @ApiParam(value = "启用 1 禁用 0", required = true) Integer enabled,
                          @ApiParam(value = "描述") String description,
                          @ApiParam(value = "角色类型 默认值1") String type) {
        Role role = roleService.add(name, enabled, description, type);
        //返回数据
        return ResultUtil.ok(new RoleListItem(role));
    }

    /**
     * 添加角色
     *
     * @param roleParam
     * @return
     */
    @ApiOperation(value = "添加角色（含功能点关联）", requestType = RequestType.JSON,response = RoleView.class)
    @PostMapping("/addRoleAndFun")
    @PreAuthorize("hasAuthority('sys::addRoleAndFun')")
    public ResultData<Role> addRoleAndFun(@RequestBody RoleParam roleParam) {
        Role role = roleService.add(roleParam.getName(), roleParam.getEnabled(), roleParam.getDescription(), roleParam.getType(), roleParam.getFunctionIds());
        return ResultUtil.ok(new RoleListItem(role));
    }

    /**
     * 编辑角色
     * @param id
     * @param name
     * @param enabled
     * @param description
     * @return
     */
    @ApiOperation("编辑角色")
    @PostMapping("/edit")
    @PreAuthorize("hasAuthority('sys::editRole')")
    public ResultData<?> edit(@ApiParam(value = "角色ID", required = true)Integer id,
                           @ApiParam(value = "角色名称", required = true) String name,
                           @ApiParam(value = "启用 1 禁用 0") Integer enabled,
                           @ApiParam(value = "描述") String description,
                           @ApiParam(value = "角色类型 默认值1") String type) {
        roleService.edit(id, name, enabled, description, type);
        return ResultUtil.ok();
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @ApiOperation("删除角色")
    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('sys::deleteRole')")
    public ResultData<?> delete(@ApiParam(value = "角色ID", required = true) Integer id) {
        roleService.delete(id);
        return ResultUtil.ok();
    }

    /**
     * 获取角色列表
     *
     * @param quickSeek
     * @param enabled
     * @return
     */
    @ApiOperation(value = "获取角色列表",response = RoleView.class)
    @PostMapping("/findList")
    public ResultData<List<RoleListItem>> findList(@ApiParam(value = "关键字") String quickSeek,
                               @ApiParam(value = "启用 1 禁用 0") Integer enabled,
                               @ApiParam(value = "角色类型 默认值 1") String type) {
        List<RoleListItem> result = roleService.findList(quickSeek, enabled, type);
        return ResultUtil.ok(result);
    }
}
